/*
 * Copyright (C) 2023 <99149230+janm012012@users.noreply.github.com> 
 * inspired and based on work
 * Copyright (C) 2011 Jason von Nieda <jason@vonnieda.org> and Cri.S <phone.cri@gmail.com>
 * 
 * This file is part of OpenPnP.
 * 
 * OpenPnP is free software: you can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * OpenPnP is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with OpenPnP. If not, see
 * <http://www.gnu.org/licenses/>.
 * 
 * For more information about OpenPnP visit http://openpnp.org
 */

/*
 * This is a tailored version of the Named CSV importer used to import Altium CSV data.
 * This is/was required because Altium uses a different schema for rotations of components
 * on the bottom side, which this importer corrects for. In addition it only checks for 
 * column headers generated by Altum, making it a little bit more robust.
 */

package org.openpnp.gui.importer;

import java.awt.Frame;

import org.openpnp.Translations;
import org.openpnp.model.Abstract2DLocatable.Side;
import org.openpnp.model.Board;
import org.openpnp.model.Location;
import org.openpnp.model.Placement;

public class AltiumCsvImporter extends CsvImporter implements BoardImporter {
    private final static String NAME = "Altium .csv"; //$NON-NLS-1$
    private final static String DESCRIPTION = Translations.getString("AltiumCsvImporter.Importer.Description"); //$NON-NLS-1$

    private Board board;

    @Override
    public String getImporterName() {
        return NAME;
    }

    @Override
    public String getImporterDescription() {
        return DESCRIPTION;
    }

    @Override
    public Board importBoard(Frame parent) throws Exception {
    	// run Named CSV importer
    	board = super.importBoard(parent);
    	
    	// modify rotation of bottom side components
    	if (board != null) {
	        for (Placement p : board.getPlacements()) {
	        	if (p.getSide() == Side.Bottom) {
	        		Location l = p.getLocation();
	        		// set new location using corrected rotation
	        		p.setLocation(new Location(l.getUnits(), l.getX(), l.getY(), l.getZ(), 180 - l.getRotation()));
	        	}
	        }
    	}
    	
    	return board;
    }

    // methods to read of strings for each purpose to be found in the heading line
    // data read from file is converted to upper case before compare -> only list upper case pattern here
    // this is the list that is used by the default Named CSV importer
    public String[] getReferencePattern() {
    	return new String[] {
    			"DESIGNATOR" 	//$NON-NLS-1$
    	};
    }
    public String[] getValuePattern() {
    	return new String[] {
    			"COMMENT"	 	//$NON-NLS-1$
    	};
    }
    public String[] getPackagePattern() {
    	return new String[] {
    			"FOOTPRINT"	 	//$NON-NLS-1$
    	};
    }
    public String[] getXPattern() {
    	return new String[] {
    			"REF-X(MM)",  	//$NON-NLS-1$
    			"REF-X(MIL)",  	//$NON-NLS-1$
    			"CENTER-X(MM)", //$NON-NLS-1$
    			"CENTER-X(MIL)"	//$NON-NLS-1$
    	};
    }
    public String[] getYPattern() {
    	return new String[] {
    			"REF-Y(MM)",  	//$NON-NLS-1$
    			"REF-Y(MIL)",  	//$NON-NLS-1$
    			"CENTER-Y(MM)", //$NON-NLS-1$
    			"CENTER-Y(MIL)"	//$NON-NLS-1$
    	};
    }
    public String[] getRotationPattern() {
    	return new String[] {
    		    "ROTATION" 		//$NON-NLS-1$
    	};
    }
    public String[] getSidePattern() {
    	return new String[] {
    		    "LAYER"			//$NON-NLS-1$
    	};
    }
    public String[] getHeightPattern() {
    	return new String[] {
    		    "HEIGHT(MIL)",	//$NON-NLS-1$ 
    		    "HEIGHT(MM)"  	//$NON-NLS-1$
    	};
    }
    public String[] getCommentPattern() {
    	return new String[] {
    		    "DESCRIPTION" 	//$NON-NLS-1$
    	};
    }
}
